package com.oocl.servlet.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.service.CustomerManager;
import com.oocl.service.factory.CustomerManagerFactory;

@WebServlet("/login.action")
public class LoginActionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private CustomerManager customerManager = CustomerManagerFactory.getInstance();

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cname = request.getParameter("cname");
		String cpwd = request.getParameter("cpwd");
		
		int result = customerManager.findCustomer(cname, cpwd);
		boolean loginSuccess = false;
		switch (result) {
		case 1:
			loginSuccess = true;
			break;
		case 0:
			request.setAttribute("errorMsg", "Password Incorrect!");
			break;
		case -1:
			request.setAttribute("errorMsg", "Customer not exist!");
			break;
		}
		
		if (loginSuccess) {
			request.getSession().setAttribute("loginCustomerName", cname);
			response.sendRedirect("sec/home");
		} else {
			request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);
		}
	}

}
